/**
 * @author 徐楠
 * @date 2022/4/23 23:31
 * @version 1.0
 */

package com.xunan.likou;

import java.util.ArrayList;
import java.util.List;

public class InvertWordsInString {
    public static void main(String[] args) {
        //String s = "Let's take LeetCode contest";
        String s = "  hello world  ";
        String result = reverseWords(s);
        System.out.println(result);
    }

    public static String reverseWords(String s) {
        String[] s1 = s.split(" ");
        List<String> list = new ArrayList<>();
        for (String s2 : s1) {
            if (!s2.equals("")) {
                list.add(s2);
            }
        }
        System.out.println(list.size());
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = list.size() - 1; i >= 0; i--) {
            stringBuffer.append(list.get(i));
            if (i != 0) {
                stringBuffer.append(" ");
            }
        }
        return stringBuffer.toString();
    }
}
